Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

194
Visualizações
how can pass different parameters using [routerLink] or router.navigate to a component?

I configured app-routing.module.ts as following:

const routes: Routes = [
  {
    path: "branches/:branch",
    component: BranchesComponent
  },
  
  // ...
];

and also in app.component.html have:

<li>
      <a [routerLink]="['/branches', 'engineering']"> engineering </a>
    </li>
    <li>
      <a [routerLink]="['/branches', 'baseSciense']"> baseSciense</a>
    </li>
    <li>
      <a [routerLink]="['/branches', 'humanities']"> humanities</a>
    </li>
  </ul>
  
  <router-outlet></router-outlet>

and in the barnches.component.ts I coded as bellow:

branch: string ='';
  
  constructor(private route: ActivatedRoute) { }

  ngOnInit(): void {
  
    this.route.params.subscribe(({branch: branch1}) => this.branch = branch1);
    
    // I also tried this code:
    // this.route.params.subscribe(branch => this.branch = branch['branch']);
    
    // unfortunately, bellow codes have error on p.branch and params.branch! why?
    // this.route.params.subscribe(p => this.branch = p.branch)
    // this.branch = this.route.snapshot.params.branch;
    
    console.log(`branch is : ${this.branch}`);
  }

till here everything comes correct, and URL is changed once the respective link is clicked, such as :

http://localhost:4200/branches/engineering

http://localhost:4200/branches/baseSciense

http://localhost:4200/branches/humanities

but the property branch in Branches component is not changed and has same value (engineering) for different parameters in log of the console. it is illogical for me!

how can solve this problem as pass different parameters to and capture them into branches component? Best regards

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You need to move your console.log within the subscription. Most of the code related to this feature will need to take place within the subscription. The Component doesn't re-render on url change because it is loading the same component and angular doesn't re-render components if it is the same component.

import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';

@Component({
  template: 'Branch: {{branch}}',
})
export class BranchesComponent implements OnInit {
  branch = '';

  constructor(private route: ActivatedRoute) {}

  ngOnInit() {
    // Set the value every time the URL param changes.
    this.route.params.subscribe(({ branch }) => {
      this.branch = branch;
      console.log('branch:', this.branch);
    });
  }
}
about 4 years ago · Juan Pablo Isaza Relatório

0

In your component you can subscribe to router events and listen for them like this.

this.router.events.pipe(filter(r => r instanceof NavigationEnd)).subscribe(r => {
  console.log((r as NavigationEnd).url);
});
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda